ConditionValidator(T) Class

CuttingEdge.Conditions

Enables validation of pre- and postconditions. This class isn't used directly by developers. Instead the class should be created by the Requires and Ensures extension methods.

Namespace:  CuttingEdge.Conditions
Assembly:  CuttingEdge.Conditions (in CuttingEdge.Conditions.dll)

Syntax

Visual Basic (Declaration)
Public MustInherit Class ConditionValidator(Of T)
C#
public abstract class ConditionValidator<T>
Visual C++
generic<typename T>
public ref class ConditionValidator abstract
JavaScript
JavaScript does not support generic types or methods.

Type Parameters

T
The type of the argument to be validated

Examples

The following example shows how to use CuttingEdge.Conditions.
 Copy Code
  using System.Collections;
  
  using CuttingEdge.Conditions;
  
  public class ExampleClass
  {
      private enum StateType { Uninitialized = 0, Initialized };
      
      private StateType currentState;
  
      public ICollection GetData(int? id, string xml, IEnumerable col)
      {
          // Check all preconditions:
          Condition.Requires(id, "id")
              .IsNotNull()          // throws ArgumentNullException on failure
              .IsInRange(1, 999)    // ArgumentOutOfRangeException on failure
              .IsNotEqualTo(128);   // throws ArgumentException on failure
  
          Condition.Requires(xml, "xml")
              .StartsWith("<data>") // throws ArgumentException on failure
              .EndsWith("</data>"); // throws ArgumentException on failure
  
          Condition.Requires(col, "col")
              .IsNotNull()          // throws ArgumentNullException on failure
              .IsEmpty();           // throws ArgumentException on failure
  
          // Do some work
  
          // Example: Call a method that should return a not null ICollection
          object result = BuildResults(xml, col);
  
          // Check all postconditions:
          // A PostconditionException will be thrown at failure.
          Condition.Ensures(result, "result")
              .IsNotNull()
              .IsOfType(typeof(ICollection));
  
          return result as ICollection;
      }
  }
  
The following code examples shows how to extend the library with your own 'Invariant' entry point method. The first example shows a class with an Add method that validates the class state (the class invariants) before adding the Person object to the internal array and that code should throw an InvalidOperationException.
 Copy Code
   using CuttingEdge.Conditions;
    
   public class Person { }
    
   public class PersonCollection  
   {
       public PersonCollection(int capicity)
       {
           this.Capacity = capicity;
       }
    
       public void Add(Person person)
       {
           // Throws a ArgumentNullException when person == null
           Condition.Requires(person, "person").IsNotNull();
            
           // Throws an InvalidOperationException on failure
           Invariants.Invariant(this.Count, "Count").IsLessOrEqual(this.Capacity);
            
           this.AddInternal(person);
       }
  
       public int Count { get; private set; }
       public int Capacity { get; private set; }
        
       private void AddInternal(Person person)
       {
           // some logic here
       }
        
       public bool Contains(Person person)
       {
           // some logic here
           return false;
       }
   }
    
The following code example will show the implementation of the Invariants class.
 Copy Code
  using System;
  using CuttingEdge.Conditions;
  
  namespace MyCompanyRootNamespace
  {
      public static class Invariants
      {
          public static ConditionValidator<T> Invariant<T>(T value)
          {
              return new InvariantValidator<T>("value", value);
          }
  
          public static ConditionValidator<T> Invariant<T>(T value, string argumentName)
          {
              return new InvariantValidator<T>(argumentName, value);
          }
  
          // Internal class that inherits from ConditionValidator<T>
          sealed class InvariantValidator<T> : ConditionValidator<T>
          {
              public InvariantValidator(string argumentName, T value)
                  : base(argumentName, value)
              {
              }
  
              protected override void ThrowExceptionCore(string condition,
                  string additionalMessage, ConstraintViolationType type)
              {
                  string exceptionMessage = string.Format("Invariant '{0}' failed.", condition);
  
                  if (!String.IsNullOrEmpty(additionalMessage))
                  {
                      exceptionMessage += " " + additionalMessage;
                  }
  
                  // Optionally, the 'type' parameter can be used, but never throw an exception
                  // when the value of 'type' is unknown or unvalid.
                  throw new InvalidOperationException(exceptionMessage);
              }
          }
      }
  }
  

Inheritance Hierarchy

System..::.Object
  CuttingEdge.Conditions..::.ConditionValidator<(Of <(T>)>)

See Also